package yuekao1.ods;

import com.alibaba.ververica.cdc.connectors.mysql.MySQLSource;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.source.SourceFunction;
import yuekao1.util.JsonDebeziumDeserializationSchema;
import yuekao1.util.KafkaUtil;

public class ReadMySql {
    public static void main(String[] args) throws Exception {
        //2）、编写Flink流式程序，使用Flink CDC实时全量+增量采集MySQL数据库表（主要业务表数据，不少于5张表）的数据，发送存储至Kafka消息队列：topic-db；（4分）
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1); // use parallelism 1 for sink to keep message ordering


        SourceFunction<String> sourceFunction = MySQLSource.<String>builder()
                .hostname("hadoop-single")
                .port(3306)
                .databaseList("yuekao01") // monitor all tables under inventory database
                //sku_info 商品、 user_info 用户、province_info order_info 和 order_detail ）
                .tableList("yuekao01.sku_info,yuekao01.user_info,yuekao01.base_province,yuekao01.order_info,yuekao01.order_detail")
                .username("root")
                .password("root")
                .deserializer(new JsonDebeziumDeserializationSchema()) // converts SourceRecord to String
                .build();


        DataStreamSource<String> streamSource = env.addSource(sourceFunction);
//        streamSource.print();

        streamSource.addSink(KafkaUtil.kafkaSink("topic_db"));
        env.execute();
    }
}
